package com.pan.crawler.impl9;

import com.pan.crawler.Page;
import com.pan.entity.Chapter;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;

import java.time.Duration;

public class PageImpl9 implements Page {


    private EdgeDriver edgeDriver = new EdgeDriver();
    {
        edgeDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
    }

    @Override
    public String get(Chapter chapter) {
        edgeDriver.navigate().to(chapter.getUrl());
        StringBuffer sbContent = new StringBuffer();
        for (;;) {
            sbContent.append(edgeDriver.findElement(By.className("content")).getText());
            WebElement nextUrl = edgeDriver.findElement(By.id("next_url"));
            if (nextUrl.getText().indexOf("下一页") == -1) {
                break;
            }
            nextUrl.click();
        }
        return sbContent.toString();
    }

    @Override
    public void close() {
        edgeDriver.quit();
    }
}
